<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>07-机器指令编程 | hez2010</title>
  
  
  
  <!--link rel="stylesheet" href="//cdn.jsdelivr.net/highlight.js/9.10.0/styles/github-gist.min.css"-->
  
<link rel="stylesheet" href="//cdn.jsdelivr.net/highlight.js/9.10.0/styles/github-gist.min.css">

  
<link rel="stylesheet" href="/css/style.css">

<!-- hexo injector head_end start -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css">

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-math@4.0.0/dist/style.css">
<!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.1.1"></head>

<body>
<div class="Shell">
    <aside class='SideBar'>
    <section class='avatar' style="background-image: url()">
        <div class='av-pic' style="background-image: url(/assets/avatar.jpg)">
        </div>
    </section>
    <section class='menu'>
        <div>hez2010</div>
        
            <div>软件工程导论</div>
        
        <ul>
          
            <a href="/" class="Btn">
              <li>Home</li>
            </a>  
          
            <a href="/archives/" class="Btn">
              <li>Archive</li>
            </a>  
          
        </ul>
    </section>
    <section class="media">
        
            
                <a target="_blank" rel="noopener" href="https://github.com/hez2010">
                    <img src="/assets/github.svg" />
                </a>
            
        
    </section>
</aside>

    <div class="container">
        <div data-pager-shell>
            <div>
  <article class='ContentView'>
    <header class='PageTitle'>
        <h1>07-机器指令编程</h1>
    </header>

    <section>
      <h1 id="Homework"><a href="#Homework" class="headerlink" title="Homework"></a>Homework</h1><h2 id="Program-with-machine-language-according-to-the-following-c"><a href="#Program-with-machine-language-according-to-the-following-c" class="headerlink" title="Program with machine language according to the following c"></a>Program with machine language according to the following c</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs c">int_8 a = <span class="hljs-number">1</span>;<br>int_8 c = a + <span class="hljs-number">3</span>;<br></code></pre></td></tr></table></figure>

<h3 id="Write-your-assembly-code-amp-machine-code"><a href="#Write-your-assembly-code-amp-machine-code" class="headerlink" title="Write your assembly code &amp; machine code"></a>Write your assembly code &amp; machine code</h3><p>汇编代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs asm">LOD #1<br>STO A<br>ADD #3<br>STO C<br></code></pre></td></tr></table></figure>

<p>首先加载立即数 1 到累加寄存器，将结果存入 A，然后加 3，再存入 C 即可。</p>
<p>机器代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs bin">00000100 00000001<br>00000101 10000011<br>00010000 00000011<br>00000101 10000101<br></code></pre></td></tr></table></figure>

<p>这里假设 A 和 C 的内存地址分别为 <code>10000011</code> 和 <code>10000101</code>。</p>
<h3 id="Explain-machine-code-execution-with-the-fetch-decode-execute-cycle"><a href="#Explain-machine-code-execution-with-the-fetch-decode-execute-cycle" class="headerlink" title="Explain machine code execution with the fetch-decode-execute cycle"></a>Explain machine code execution with the fetch-decode-execute cycle</h3><p>在一个取指-译码-执行循环中：</p>
<ol>
<li>PC 寄存器中保存了下一条指令的地址，CPU 从内存中读入指令并存入 IR 寄存器，即为取指。</li>
<li>CPU 根据 IR 寄存器中的指令，将其内容解析为操作码和操作数等，即为译码。</li>
<li>CPU 根据解析出的指令，根据不同的操作码执行不同的命令，并传送操作数中的内容作为参数，即为执行。例如让 ALU 单元进行加法计算等等。</li>
</ol>
<h3 id="Explain-functions-about-IR-PC-ACC-registers-in-a-CPU"><a href="#Explain-functions-about-IR-PC-ACC-registers-in-a-CPU" class="headerlink" title="Explain functions about IR, PC, ACC registers in a CPU"></a>Explain functions about IR, PC, ACC registers in a CPU</h3><ul>
<li>IR 寄存器：指令寄存器，存储当前执行的指令内容</li>
<li>PC 寄存器：存储下一条指令的地址，CPU 将会从该地址读取下一条指令</li>
<li>ACC 寄存去：累加寄存器，该寄存器可用于对数值进行累加，常用于存放计算的中间结果等</li>
</ul>
<h3 id="Explain-physical-meaning-about-vars-a-amp-c-in-a-machine"><a href="#Explain-physical-meaning-about-vars-a-amp-c-in-a-machine" class="headerlink" title="Explain physical meaning about vars a &amp; c in a machine"></a>Explain physical meaning about vars a &amp; c in a machine</h3><p>变量 <code>a</code> 和 <code>c</code> 的物理意义对应了再内存中不同的地址。编译器将 C 语言编译后，这些变量便被赋予了不同的内存地址，这些地址是这些变量的起始内存地址，根据不同的变量类型占有不同的长度。在 CPU 对这些变量进行操作时，需要从内存中读取到寄存器，或从寄存器写入到内存的相应地址的块中。</p>
<a id="more"></a>
<h2 id="简答题"><a href="#简答题" class="headerlink" title="简答题"></a>简答题</h2><h3 id="What-are-stored-in-memory"><a href="#What-are-stored-in-memory" class="headerlink" title="What are stored in memory?"></a>What are stored in memory?</h3><p>内存中存储了程序的指令和数据。指令用于指导 CPU 完成相应的操作，数据则保存了程序的状态（如栈和堆等）。</p>
<h3 id="Can-a-data-or-a-instruction-stored-in-the-same-place"><a href="#Can-a-data-or-a-instruction-stored-in-the-same-place" class="headerlink" title="Can a data or a instruction stored in the same place?"></a>Can a data or a instruction stored in the same place?</h3><p>可以的。</p>
<p>指令和数据在内存中并没有本质上的区别，其在内存中都是以二进制的形式存储的，对应低电平（0）和高电平（1），因此一块内存中存储的到底是指令还是数据，完全看如何解读这一块内存。</p>
<p>不过由于指令和数据的作用不同，一遍编译器会将指令段和数据段划分开，并且操作系统在载入程序时也会将指令（代码）和数据划分开，并为其赋予不同的权限（例如用户程序不能修改代码段的内容）。</p>
<h3 id="Explain-Instruction-Format-with-example-instructions"><a href="#Explain-Instruction-Format-with-example-instructions" class="headerlink" title="Explain Instruction Format with example instructions"></a>Explain Instruction Format with example instructions</h3><p>指令的一般由操作码和操作数组成。</p>
<p>PIPPIN 的单条指令大小为 2 个字节，第一个字节表示操作码，第二个字节表示操作数。</p>
<p>例如取数指令：<code>LOD #n</code>，其指令的二进制形式为 <code>00000101 zzzzzzzz</code>。</p>
<p><code>00000101</code> 即为操作码，而 <code>zzzzzzzz</code> 即为操作数，CPU 在执行该命令时，通过操作码确定要进行取数命令，而通过操作数决定要取的内存地址。</p>
<p>又比如加法指令，则由 <code>00010000 bbbbbbbb</code> 表示给累加寄存器加 <code>bbbbbbbb</code>，这个操作由 <code>00010000</code> 来确定；以及 <code>00000000 bbbbbbbb</code> 表示给累加寄存器加 <code>bbbbbbbb</code> 内存地址下保存的值，该操作由 <code>00000000</code> 来确定。</p>
<h2 id="解释以下词汇"><a href="#解释以下词汇" class="headerlink" title="解释以下词汇"></a>解释以下词汇</h2><h3 id="汇编语言（Assembly-Language）"><a href="#汇编语言（Assembly-Language）" class="headerlink" title="汇编语言（Assembly Language）"></a>汇编语言（Assembly Language）</h3><p>In computer programming, assembly language (or assembler language), often abbreviated asm, is any low-level programming language in which there is a very strong correspondence between the instructions in the language and the architecture’s machine code instructions.</p>
<p>Because assembly depends on the machine code instructions, every assembly language is designed for exactly one specific computer architecture. Assembly language may also be called symbolic machine code.</p>
<p>这是一种低级编程语言，在该语言中，语言中的指令与体系结构的机器代码指令之间存在非常强的对应关系。因为汇编取决于机器代码指令，所以每种汇编语言都是针对一种特定的计算机体系结构而设计的。汇编语言也可以称为符号机器代码。</p>
<h3 id="编译（Compiler）"><a href="#编译（Compiler）" class="headerlink" title="编译（Compiler）"></a>编译（Compiler）</h3><p>In computing, a compiler is a computer program that translates computer code written in one programming language (the source language) into another language (the target language). The name “compiler” is primarily used for programs that translate source code from a high-level programming language to a lower level language (e.g., assembly language, object code, or machine code) to create an executable program.</p>
<p>编译器是一种计算机程序，可以将以一种编程语言编写的计算机代码转换为另一种语言。编译器主要用于将源代码从高级编程语言转换为低级语言（例如，汇编语言，目标代码或机器代码）以创建可执行程序的程序。</p>
<h3 id="命令式语言（Imperative-programming）"><a href="#命令式语言（Imperative-programming）" class="headerlink" title="命令式语言（Imperative programming）"></a>命令式语言（Imperative programming）</h3><p>In computer science, imperative programming is a programming paradigm that uses statements that change a program’s state. In much the same way that the imperative mood in natural languages expresses commands, an imperative program consists of commands for the computer to perform. Imperative programming focuses on describing how a program operates.</p>
<p>命令式编程使用更改程序状态的语句，由计算机执行的命令组成，着重于描述程序的运行方式。</p>
<h3 id="函数编程语言（Functional-programming）"><a href="#函数编程语言（Functional-programming）" class="headerlink" title="函数编程语言（Functional programming）"></a>函数编程语言（Functional programming）</h3><p>In computer science, functional programming is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions that each return a value, rather than a sequence of imperative statements which change the state of the program.</p>
<p>函数式编程通过应用和组合函数来构造程序。其中函数定义是每个返回一个值的表达式树，而不是一系列更改程序状态的命令性语句。</p>
<h3 id="过程式编程（Procedural-programming）"><a href="#过程式编程（Procedural-programming）" class="headerlink" title="过程式编程（Procedural programming）"></a>过程式编程（Procedural programming）</h3><p>Procedural programming is a programming paradigm, derived from structured programming, based on the concept of the procedure call. Procedures (a type of routine or subroutine) simply contain a series of computational steps to be carried out. Any given procedure might be called at any point during a program’s execution, including by other procedures or itself.</p>
<p>过程式编程是一种基于程序调用概念的结构化编程派生的编程模式。过程仅包含要执行的一系列计算步骤，任何给定的过程都可以在程序执行过程中的任何时候调用，包括其他过程或其本身。</p>


      

    </section>
    
      <section class='ArticleMeta'>
          <div>
            发布于&nbsp;
            <time datetime="2020-11-07T08:38:49.282Z" itemprop="datePublished">
              2020-11-07
            </time>
          </div>
          
      </section>
    
    
</article>

  
</div>

            <footer>
    <div>© 2021 - Steven He </div>
    <div>
        <span>
            Powered by <a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a>
        </span>
        ,
        <span>
            Theme - <a target="_blank" rel="noopener" href="https://github.com/nameoverflow/hexo-theme-icalm">Icalm</a>
        </span>
    </div>
</footer>

        </div>
    </div>
</div>

<script src="/js/pager/dist/singlepager.js"></script>

<script>
var sp = new Pager('data-pager-shell')

</script>
</body>
</html>